package com.sf.webside.controller;

import com.sf.webside.bean.CmsRquestBean;
import com.sf.webside.service.CmsArticleService;
import entity.ResultVO;
import entity.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import util.GsonUtils;
import util.ResultVOUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by tanjing on 2019/2/21.
 */
@RestController
@RequestMapping("/v1/cms/article")
@ResponseBody
public class CmsArticleController extends BaseExceptionHandler{
    private Logger logger= LoggerFactory.getLogger(CmsArticleController.class);
    @Autowired
    private CmsArticleService cmsArticleService;
    /**
     * 获取文章列表
     */
    @GetMapping("/list/{categoryId}/{languageType}/{page}/{size}")
    public Object list(HttpServletRequest request,@PathVariable String categoryId,@PathVariable String languageType, @PathVariable int page, @PathVariable int size){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("list request {},{},{}",categoryId,page,size);
        if(StringUtils.isBlank(categoryId)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入栏目");
        }
        if(StringUtils.isBlank(languageType)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入语言");
        }
        if(page<=0){
            page=0;//默认从0开始
        }else{
            page=page-1;
        }
        if(size<=0){
            size=20;//默认20条；
        }
        ResultVO resultVO=cmsArticleService.list(categoryId,languageType,page,size);
        logger.info("list response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 获取文章详情
     */
    @GetMapping("/getActicelCotent/{id}")
    public Object getActicelCotent(HttpServletRequest request,@PathVariable String id){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("getActicelCotent request {}",id);
        if(StringUtils.isBlank(id)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入文章编号");
        }
        ResultVO resultVO=cmsArticleService.getActicelCotent(id);
        logger.info("getActicelCotent response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 保存文章
     */
    @PostMapping("/save")
    public Object save(HttpServletRequest request,CmsRquestBean cmsRquestBean){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("save request {}", GsonUtils.getInstance().toJson(cmsRquestBean));
        String title=cmsRquestBean.getTitle();
        String categoryId=cmsRquestBean.getCategoryId();
        String languageType=cmsRquestBean.getLanguageType();
        if(StringUtils.isBlank(title)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入标题");
        }
        if(StringUtils.isBlank(categoryId)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入栏目");
        }
        if(StringUtils.isBlank(languageType)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入语言");
        }
        ResultVO resultVO=cmsArticleService.save(cmsRquestBean);
        logger.info("save response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 修改文章
     */
    @PostMapping("/update/{id}")
    public Object update(HttpServletRequest request,CmsRquestBean cmsRquestBean,@PathVariable String id){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("update request {}", GsonUtils.getInstance().toJson(cmsRquestBean));
        String title=cmsRquestBean.getTitle();
        String categoryId=cmsRquestBean.getCategoryId();
        String languageType=cmsRquestBean.getLanguageType();
        if(StringUtils.isBlank(id)){
            return ResultVOUtil.error(StatusCode.ERROR,"文章编号不能为空");
        }
        if(StringUtils.isBlank(title)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入标题");
        }
        if(StringUtils.isBlank(categoryId)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入栏目");
        }
        if(StringUtils.isBlank(languageType)){
            return ResultVOUtil.error(StatusCode.ERROR,"请输入语言");
        }
        cmsRquestBean.setId(id);
        ResultVO resultVO=cmsArticleService.update(cmsRquestBean);
        logger.info("update response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 删除文章
     */
    @GetMapping("/delete/{id}")
    public Object delete(HttpServletRequest request,@PathVariable String id){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("delete request {}", id);
        if(StringUtils.isBlank(id)){
            return ResultVOUtil.error(StatusCode.ERROR,"文章编号不能为空");
        }
        ResultVO resultVO=cmsArticleService.delete(id);
        logger.info("delete response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 文件上传
     */
    @PostMapping("/fileUpload")
    public Object fileUpload(HttpServletRequest request){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("fileUpload request ");
        MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
        ResultVO resultVO=cmsArticleService.fileUpload(multipartHttpServletRequest);
        logger.info("fileUpload response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 文件上传列表
     */
    @GetMapping("/fileList")
    public Object fileUploadList(HttpServletRequest request){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("fileUploadList request ");
        ResultVO resultVO=cmsArticleService.fileUploadList();
        logger.info("fileUploadList response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 文件上传删除
     */
    @GetMapping("/fileDelete/{fileName}")
    public Object fileUploadDelete(HttpServletRequest request,@PathVariable String fileName){
        ResultVO checkoutToken=checkoutToken(request);
        if(checkoutToken.getCode()!=200) {
            return checkoutToken;
        }
        logger.info("fileUploadDelete request {}",fileName);
        ResultVO resultVO=cmsArticleService.fileUploadDelete(fileName);
        logger.info("fileUploadDelete response "+ GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
